package cn.rxls.community.controller;

import cn.rxls.community.dto.PageDTO;
import cn.rxls.community.dto.activity.ActivityPageDTO;
import cn.rxls.community.entity.Activity;
import cn.rxls.community.enums.ActivitySateEnum;
import cn.rxls.community.enums.MaintenanceStateEnum;
import cn.rxls.community.result.Result;
import cn.rxls.community.vo.activity.ActivityVO;
import cn.rxls.community.service.ActivityService;
import cn.rxls.community.vo.category.CategoryVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 活动(Activity)表
 */
@RestController
@RequestMapping("activity")
@Validated
public class ActivityController {
    /**
     * 服务对象
     */
    @Resource
    private ActivityService activityService;

    /**
     * 根据分类分页查询所有活动数据
     *
     * @param pageDTO 分页对象
     * @return 所有数据
     */
    @GetMapping("/page/category")
    public Result<?> selectPageCategory(@ModelAttribute @Valid ActivityPageDTO pageDTO) {
        //对分页参数进行处理
        PageDTO page = new PageDTO();
        Activity activity = new Activity();
        page.setPage((pageDTO.getPage() - 1) * pageDTO.getSize());
        page.setSize(pageDTO.getSize());
        activity.setActivityCategory(pageDTO.getCategoryId());
        return Result.success(activityService.selectPageActivityCategory(page, activity));
    }


    /**
     * 根据状态分页查询所有活动数据
     *
     * @param pageDTO 分页对象
     * @return 所有数据
     */
    @GetMapping("/page/state")
    public Result<?> selectPageAll(@ModelAttribute @Valid ActivityPageDTO pageDTO) {
        //对分页参数进行处理
        PageDTO page = new PageDTO();
        Activity activity = new Activity();
        page.setPage((pageDTO.getPage() - 1) * pageDTO.getSize());
        page.setSize(pageDTO.getSize());
        Integer state = ActivitySateEnum.getCodeByMsg(pageDTO.getState());
        activity.setActivityState(state);
        return Result.success(activityService.selectPageActivityCategory(page, activity));
    }

    /**
     * 根据id查询活动详情数据
     *
     * @param id 活动id
     * @return 所有数据
     */
    @GetMapping
    public Result<ActivityVO> selectById(@RequestParam(value = "id", required = false)
                                         @NotNull(message = "非法活动id")
                                         @Min(value = 1, message = "非法活动id") Long id) {
        return Result.success(activityService.selectActivityDetail(id));
    }


    /**
     * 获取所有的活动分类
     *
     * @return 活动分类列表
     */
    @GetMapping("/category")
    public Result<List<CategoryVO>> selectCategory() {
        return Result.success(activityService.selectActivityAllCategory());
    }

}

